package maldade.service;

import java.util.List;

import maldade.persistence.dao.ExampleDAO;
import maldade.persistence.exception.TransactionException;
import maldade.persistence.model.Example;

/**
 * Example service class.
 * 
 * @author rnojiri
 */
public class ExampleService extends AbstractService
{
	/**
	 * Calls the parent constructor.
	 */
	public ExampleService()
	{
		super();
	}
	
	/**
	 * Saves a new Example.
	 * 
	 * @param example
	 */
	public void saveExample(Example example)
	{
		try
		{
			persistenceService.beginTransaction();
			
			ExampleDAO exampleDAO = new ExampleDAO();
			
			exampleDAO.saveOrUpdate(example);
			
			persistenceService.commit();
		}
		catch (TransactionException e) {
			
			log.error("Error saving example!", e);
		}
	}
	
	/**
	 * Returns all examples.
	 * 
	 * @return List<Example>
	 */
	public List<Example> getAllExamples()
	{
		try
		{
			persistenceService.beginTransaction();
			
			ExampleDAO exampleDAO = new ExampleDAO();
			
			List<Example> results = exampleDAO.getAllExamples();
			
			persistenceService.commit();
			
			return results;
		}
		catch (TransactionException e) {
			
			log.error("Error getting all examples!", e);
		}
		
		return null;
	}
	
	/**
	 * Returns examples by name.
	 * 
	 * @return List<Example>
	 */
	public List<Example> getExamplesByName(String nameLike)
	{
		try
		{
			persistenceService.beginTransaction();
			
			ExampleDAO exampleDAO = new ExampleDAO();
			
			List<Example> results = exampleDAO.getExamplesByName(nameLike);
			
			persistenceService.commit();
			
			return results;
		}
		catch (TransactionException e) {
			
			log.error("Error getting examples by name!", e);
		}
		
		return null;
	}
}
